package com.rent.auth.controller;

import com.log.annotation.SysLog;
import com.rent.auth.model.Admin;
import com.rent.auth.pojo.AuthToken;
import com.rent.auth.pojo.LoginForm;
import com.rent.auth.service.IAdminService;
import com.rent.common.annotation.AvoidRepeatableCommit;
import com.rent.common.api.ApiString;
import com.rent.common.pojo.ResultJSON;
import com.rent.common.utils.CommonUtil;
import com.rent.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zn
 * @since 2023-07-29
 */
@Slf4j
@RestController
@RequestMapping("/admin")
@Api(value = "AdminController", tags = "账户相关接口")
public class AdminController extends MyBaseController<IAdminService, Admin, Integer> {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IAdminService adminService;

    /**
     * 认证密码模式  登录
     *
     * @return 结果
     */
    @AvoidRepeatableCommit(timeout = 10000)
    @SysLog(value = "用户登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "用户登录", httpMethod = "POST")
    public ResultJSON<AuthToken> adminLogin(@RequestBody @Validated LoginForm loginForm) {
        if (loginForm == null) return ResultJSON.failure(ApiString.loginFail);
        // 校验验证验证码
        String verifyCode = StringUtils.trim(loginForm.getVerifyCode());
        boolean verifyStatus = checkVerifyCodeIsRight(verifyCode);
        if (!verifyStatus) return ResultJSON.failure(ApiString.verificationCodeCheckFail);
        // 请求token
        AuthToken authToken = adminService.authLogin(loginForm);
        if (authToken == null) return ResultJSON.failure(ApiString.usernameOrPasswordError);
        // 返回登录成功状态
        return ResultJSON.success(ApiString.loginSuccess, authToken);
    }

    /**
     * 退出登录 - 清除token及登录信息
     *
     * @return 结果
     */
    @AvoidRepeatableCommit(timeout = 5000)
    @SysLog(value = "用户登出")
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ApiOperation(value = "用户登出", httpMethod = "POST")
    public ResultJSON<String> adminLogout() {
        adminService.logout();
        return ResultJSON.success(ApiString.logoutSuccess);
    }

    /**
     * 核验验证码
     */
    protected boolean checkVerifyCodeIsRight(String verifyCode) {
        verifyCode = StringUtils.trim(verifyCode);
        if (StringUtils.isEmpty(verifyCode)) return false;
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String key = verifyCode + "-" + CommonUtil.getIPAddress(request);
        String code = redisTemplate.opsForValue().getAndDelete(key);
        log.info("获取并删除缓存的验证码:{}", code);
        if (StringUtils.isEmpty(code)) return false;
        return verifyCode.equalsIgnoreCase(code);
    }


}
